package com.palm.hubo2o.ui.fragment;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.palm.hubo2o.R;
import com.palm.hubo2o.adapter.BallGroupAdapter;
import com.palm.hubo2o.event.BetEvent;
import com.palm.hubo2o.model.Bet;
import com.palm.hubo2o.util.EnumCode;
import com.palm.hubo2o.view.GridItemDecoration;
import com.palm.hubo2o.view.K3BallView;
import com.palm.palmutils.LayoutUtils;
import com.palm.palmutils.widget.EasyRecyclerView;

import butterknife.Bind;
import de.greenrobot.event.EventBus;

/**
 * Created by dest1 on 2015/10/14.
 * 和值
 */
public class K3SumFragment extends LotteryFragment implements EasyRecyclerView.CustomChoiceListener, EasyRecyclerView.OnItemClickListener {
    @Bind(R.id.recycler_view)
    EasyRecyclerView mGrid;
    private GridLayoutManager manager;

    public final int[] awards = {80, 40, 25, 16, 12, 10, 9, 9, 10, 12, 16, 25, 40, 80};
    private final String[] special = {"大", "小", "单", "双"};//<=10为小 下标7

    private final Bet bet = new Bet(EnumCode.gameId.HBP3);

    @Override
    protected void onClearChoices() {
        mGrid.clearChoices();
        bet.getReds().clear();
    }

    @Override
    public boolean onRandomBalls() {
        return false;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onCreateView2(View view, Bundle savedInstanceState) {
//        mGrid.setChoiceMode(EasyRecyclerView.CHOICE_MODE_MULTIPLE);
        mGrid.setChoiceMode(EasyRecyclerView.CHOICE_MODE_MULTIPLE_CUSTOM);
        manager = new GridLayoutManager(getContext(), 4);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int type = mGrid.getAdapter().getItemViewType(position);
                return type == BallGroupAdapter.ITEM_TYPE.ITEM_TYPE_MARK.ordinal() ? manager.getSpanCount() : 1;
            }
        });
//        mGrid.setSelector(RippleSalon.generateRippleDrawable(false));
        mGrid.setDrawSelectorOnTop(true);
        mGrid.addItemDecoration(new GridItemDecoration(LayoutUtils.dp2pix(getContext(), 2)));
        mGrid.setLayoutManager(manager);
        mGrid.setCustomCheckedListener(this);
        mGrid.setOnItemClickListener(this);
        mGrid.intoCustomChoiceMode();//开启选中模式
        mGrid.setAdapter(adapter);
        adapter.setBallNumber(awards.length, special.length);
        bet.setPlayType(EnumCode.PlayType.K3_HZ);
    }

    @Override
    public void onDestroyView() {
        /*bug of recyclerview and GridLayoutManager with SpanSizeLookup*/
        ((GridLayoutManager) mGrid.getLayoutManager()).setSpanSizeLookup(null);
        mGrid.setLayoutManager(null);
        mGrid.setAdapter(null);
        super.onDestroyView();
    }


    @Override
    public void onIntoCustomChoice(EasyRecyclerView view) {

    }

    @Override
    public void onOutOfCustomChoice(EasyRecyclerView view) {

    }

    @Override
    public boolean onItemClick(EasyRecyclerView parent, View view, int position, long id) {
        parent.toggleItemChecked(position);
        return true;
    }

    @Override
    public void onItemCheckedStateChanged(EasyRecyclerView view, int position, long id, boolean checked) {
        //球状态改变操作
        int index = adapter.getItemGroupIndex(position);
        int pos = adapter.getPositionInGroup(position, index);
        if (index == 0) {
            K3BallView ballView = (K3BallView) view.findViewHolderForAdapterPosition(position).itemView;
            String ballText = ballView.getNumTex();
            if (checked) {
                bet.getReds().add(ballText);
            } else {
                bet.getReds().remove(ballText);
            }
            if (bet.getReds().size() > 0) {
                bet.setWagerCount(bet.getReds().size());
                EventBus.getDefault().post(new BetEvent(bet));
            } else {
                EventBus.getDefault().post(new BetEvent());
            }
        } else {//大小单双状态改变
            if (pos == 0) {//大
                if (checked && view.isItemChecked(adapter.transToRawPosition(1, 1)))
                    view.setItemChecked(adapter.transToRawPosition(1, 1), false);//大小互斥
            } else if (pos == 1 && view.isItemChecked(adapter.transToRawPosition(0, 1))) {//小
                if (checked) view.setItemChecked(adapter.transToRawPosition(0, 1), false);//大小互斥
            } else if (pos == 2 && view.isItemChecked(adapter.transToRawPosition(3, 1))) {//单
                if (checked) view.setItemChecked(adapter.transToRawPosition(3, 1), false);//单双互斥
            } else if (pos == 3 && view.isItemChecked(adapter.transToRawPosition(2, 1))) {//双
                if (checked) view.setItemChecked(adapter.transToRawPosition(2, 1), false);//单双互斥
            }
            changeCheckValue(view);
        }


    }

    //大(小)单(双)
    private void changeCheckValue(EasyRecyclerView view) {
        boolean da = view.isItemChecked(adapter.transToRawPosition(0, 1));
        boolean xiao = view.isItemChecked(adapter.transToRawPosition(1, 1));
        boolean dan = view.isItemChecked(adapter.transToRawPosition(2, 1));
        boolean shuang = view.isItemChecked(adapter.transToRawPosition(3, 1));
        for (int i = 0; i < awards.length; i++) {
            int realPos = adapter.transToRawPosition(i, 0);
            view.setItemChecked(realPos
                    , (da || xiao || dan || shuang) && ((da == xiao) || (ifDa(i, da) || ifXiao(i, xiao)))
                            && ((dan == shuang) || (ifDan(i, dan) || ifShuang(i, shuang))));
        }


    }

    private boolean ifDa(int number, boolean ifDa) {
        return ifDa && number >= awards.length / 2;
    }

    private boolean ifXiao(int number, boolean ifXiao) {
        return ifXiao && number < awards.length / 2;
    }

    private boolean ifDan(int number, boolean ifDan) {
        return ifDan && number % 2 != 0;
    }

    private boolean ifShuang(int number, boolean ifShuang) {
        return ifShuang && number % 2 == 0;
    }

    private BallGroupAdapter adapter = new BallGroupAdapter() {

        @Override
        public BallHolder onCreateItemHolder(ViewGroup parent) {
            K3BallView ball = new K3BallView(parent.getContext());
            return new BallHolder(ball);
        }

        @Override
        public BallHolder onCreateLabelHolder(ViewGroup parent) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_k3_mark, parent, false);
            return new BallHolder(v);
        }

        @Override
        public void onBindItemViewHolder(BallHolder holder, int groupIndex, int position) {
            K3BallView ball = (K3BallView) holder.itemView;
            if (groupIndex == 0) {
                String text = position + 4 + "";
                ball.setNumText(text);
                ball.setAward(awards[position]);

            } else {
                ball.setNumText(special[position]);
                ball.showInfo(false);
            }


        }


        @Override
        public void onBindMarkViewHolder(BallHolder holder, int groupIndex) {
            TextView textView = (TextView) holder.itemView.findViewById(R.id.title_mark);
            textView.setText(groupIndex == 0 ? "猜开奖号码相加的和" : null);
        }


    };

}
